//
//  ClassTabAndColViewController.m
//  Luck
//
//  Created by 纳里健康 on 2018/5/22.
//  Copyright © 2018年 songK. All rights reserved.
//

#import "LGClassTabAndColViewController.h"
#import "LGClassTabAndColViewController+Collection.h"
#import "LGSearchModel.h"
#import "LGCateModel.h"
#import "LGGoodsModel.h"


@interface LGClassTabAndColViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *_tableDataArray;
    
    NSIndexPath *_selectIndex;
    
    UIView *_tagView;
}

@property (nonatomic, strong)LGSearchModel *searchModel;



@end

@implementation LGClassTabAndColViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self makeTabelData];
    
    [self makeCollectionView];
    
}

- (void)makeTagView
{
    _tagView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 50)];
    _tagView.backgroundColor = UIColorFromRGB(0xAD9B6A);
    UITableViewCell *cell = [_tableView cellForRowAtIndexPath:_selectIndex];
    [cell.contentView addSubview:_tagView];
}

- (void)makeTabelData
{
    _tableDataArray = [NSMutableArray array];
    
    [[MainApi sharedInstance] getPath:@"Index/searchInfo" params:nil resultBlock:^(NSDictionary *responseObject, NSError *error) {
        NSInteger code = [responseObject[@"code"] integerValue];
        
        if(code == 1)
        {
            NSDictionary *dict = responseObject[@"data"];
            
            self.searchModel = [LGSearchModel yy_modelWithJSON:dict];
        }
        
        for (LGCateModel *model in self.searchModel.cate_list) {
            
            NSString *name = model.cate_name;
            
            [self->_tableDataArray addObject:name];
            
        }
        self->_selectIndex = [NSIndexPath indexPathForRow:0 inSection:0];
        
        [self makeTableView];
        
        [self maekCollectionData];
        
    }];
}

- (void)maekCollectionData
{
    LGCateModel *cateModel = self.searchModel.cate_list[_selectIndex.row];
    
    [[MainApi sharedInstance] getPath:@"Index/cateList" params:@{@"pid":cateModel.cate_id} resultBlock:^(NSDictionary *responseObject, NSError *error) {
        NSInteger code = [responseObject[@"code"] integerValue];
        
        if(code == 1)
        {
            NSArray *goodArray = responseObject[@"data"];
            
            for (NSDictionary *goodDict in goodArray) {
                
                LGCateModel *model = [LGCateModel yy_modelWithJSON:goodDict];
                
                [self.productDataArray addObject:model];
            }
            
            [self.collectionView reloadData];
        }
        
    }];
}

- (void)makeTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 120, 50 * _tableDataArray.count) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
}

#pragma mark -- tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"tableViewCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    cell.textLabel.text = _tableDataArray[indexPath.row];
    
    cell.textLabel.textColor = UIColorFromRGB(0x333333);
    
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    
    cell.backgroundColor = WHITE_COLOR;
    
    if(_selectIndex == indexPath)
    {
        cell.backgroundColor = UIColorFromRGB(0xF6F6F6);
        cell.textLabel.textColor = UIColorFromRGB(0xAD9B6A);
        _tagView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 50)];
        _tagView.backgroundColor = UIColorFromRGB(0xAD9B6A);
        [cell.contentView addSubview:_tagView];
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [UIView animateWithDuration:0.2 animations:^{
        
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:self->_selectIndex];
        
        cell.backgroundColor = WHITE_COLOR;
        cell.textLabel.textColor = UIColorFromRGB(0x333333);
        
        self->_selectIndex = indexPath;
        
        UITableViewCell *nextCell = [self.tableView cellForRowAtIndexPath:self->_selectIndex];
        
        CGRect currentCellRect = [self.tableView rectForRowAtIndexPath:self->_selectIndex];
        
        nextCell.backgroundColor = UIColorFromRGB(0xF6F6F6);
        nextCell.textLabel.textColor = UIColorFromRGB(0xAD9B6A);
        
        self->_tagView.y = currentCellRect.origin.y;
    }];

    
    [self showSelectTagInCollection];
}

//替换collectionView数据源
- (void)showSelectTagInCollection
{
//    LGCateModel *cateModel = self.searchModel.cate_list[_selectIndex.row];
//
//    NSString *cateID = cateModel.cate_id;
    
    [self maekCollectionData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
